var layer;
var form;
var table;
var colorpicker;
var laydate;
var tempId;
var dictSetting;
var layIndex;
var columnObjName = "column_dict";
var columnArray = ['dictName','dictCode','dictType','createDate','updateDate','remarks'];
var interUrl = {
		select: $projectPath+'/sys/dict/select',
		save: $projectPath+'/sys/dict/save',
		del: $projectPath+'/sys/dict/delete',
		delBatch: $projectPath+'/sys/dict/deleteBatch',
		selectSetting: $projectPath+'/sys/dict/selectSetting',
		saveSetting: $projectPath+'/sys/dict/saveSetting',
		delSetting: $projectPath+'/sys/dict/deleteSetting',
		updateSettingState: $projectPath+"/sys/dict/updateSettingState",
}
/**	
 * layui 组件引入
 * @returns
 */
layui.use([ 'layer',  'form',  'table', 'laydate',  'colorpicker' ],  function() {
	layer = layui.layer;
	form = layui.form;
	table = layui.table;
	laydate = layui.laydate; 
	colorpicker = layui.colorpicker;
    
	// 执行一个 table 实例
	table.render({
		elem : '#data_table', 
		id : 'data_table', 
		url : interUrl.select, 
		title : '字典表', 
		height:'full-120', 
		//size:"sm", 
		page : true,
		toolbar : "#toolberLeft", 
		cols : [[
			{type : 'checkbox', fixed : 'left'},  
			{field : 'dictName', title : '字典名称', width: '150', align : 'center', hide: setColumnHide("dictName")}, 
			{field : 'dictCode', title : '字典编码', width: '150', align : 'center', sort : true, hide: setColumnHide("dictCode")}, 
			{field : 'dictType', title : '标签类型', width: '150', sort : true, align : 'center', hide: setColumnHide("dictType"), 
				templet: function (d) {
					if(d.dictType == 1){
						return  "单选框";
					}if(d.dictType == 2){
						return  "复选框";
					}else{
						return "下拉框";
					}
				}
			},  
			{field : 'createDate', align : 'center', width: '200', title : '创建时间', hide: setColumnHide("createDate")}, 
			{field : 'updateDate', align : 'center', width: '200', title : '更新时间', hide: setColumnHide("updateDate")}, 
			{field : 'remarks', align : 'center', width: '200', title : '备注', hide: setColumnHide("remarks")}, 
			{fixed : isRight(), align : 'center', width: '250', title : '操作', toolbar : '#toolRow'} 
		]]
	});
	
	// 头部左侧事件
	table.on('toolbar(data_table)',  function(obj) {
		if (obj.event === 'search') {
			openWindowT('search_form', "条件搜索", '100%', '150px', false, 0, false, false, null, 0.2, true)
		}else if(obj.event === 'add'){
			openWindow('data_form', "字典新增", '500px', '500px', false, 0, false, false, null, 0.2, false)
		}else if(obj.event === 'delete'){
            deleteBatch(table, 'data_table', "id", interUrl.delBatch)
		}
	})
	
	// 行监听事件
	table.on('tool(data_table)',  function(obj) {
		var data = obj.data;
		if (obj.event === 'del') {
			deleteRow(table, 'data_table', data, interUrl.del);
		}else if(obj.event === 'edit') {
			openWindow('data_form', "字典编辑", '500px', '500px', false, 0, false, false, data, 0.2, false)
		}else if(obj.event === 'detail') {
			openWindow('data_form', "字典查看", '500px', '500px', false, 0, false, false, data, 0.2, false)
		}else if(obj.event === 'setting') {
			tempId = data.id;
			dictSetting.openSetting(tempId);
		} 
	});
	
	// 查询
	form.on('submit(submitSearch)',  function(data) {
		reloadTable(table, 'data_table', data.field);
		return false;
	})
	
	// 提交
	form.on('submit(submitForm)',  function(data) {
		saveData(table, 'data_table', data.field, interUrl.save, '#data_form');
		return false;
	})

	// 字典参数设置
	dictSetting = {
		openSetting : function(did){
			openWindowRB('setting_form', "<i class='layui-icon layui-icon-set' style='position: relative;right: 3px;'></i>字典参数设置", '600px', '100%', false, 0, false, false, null, 0.2, false, function(){
				// 执行一个 table 实例
				table.render({
					elem : '#setting_table', 
					id : 'setting_table', 
					url : interUrl.selectSetting+"?did="+did, 
					height:'full-230', 
					page : true, // 开启分页
					toolbar : "#toolberLeftSetting", 
					defaultToolbar: false, // ['filter', 'print', 'exports']
					cols : [ [ // 表头
						{field : 'sort', title : '排序号', width: '80', align : 'center', hide: setColumnHide("sort"), fixed: 'left'}, 
						{field : 'remarks', title : '名称', align : 'center', width: '80', hide: setColumnHide("remarks")}, 
						{field : 'code', title : '参数编码', width: '100', align : 'center', hide: setColumnHide("code")}, 
						{field : 'state', title : '状态', width: '100', align : 'center', hide: setColumnHide("state"), toolbar : '#switchRow'},  
						{field : 'dictColor', align : 'center', width: '100', title : '颜色', hide: setColumnHide("dictColor"),
							templet: function (d) {
								return '<span style="background:'+d.dictColor+';width:10px;height:10px;display: inline-block;"></span>';
							}
						}, 
						{align : 'center', width: '100', title : '操作', toolbar : '#toolRowSetting'} 
						// fixed : 'right',
					] ]
				});
				// 头部左侧事件
				table.on('toolbar(setting_table)',  function(obj) {
					if(obj.event === 'add'){
						layIndex = openWindow('setting_data_form', "参数添加", '500px', '500px', false, 0, false, false, null, 0.2, false, function(){
							// 颜色
							colorpicker.render({
								elem: '#color-form'
							    ,color: '#65c0dc'
							    ,done: function(color){
							      $('#color-form-input').val(color);
							    }
							});
						})
					}
				})
				
				// 行监听事件
				table.on('tool(setting_table)',  function(obj) {
					var data = obj.data;
					if (obj.event === 'del') {
						deleteRow(table, 'setting_table', data, interUrl.delSetting);
					}else if(obj.event === 'edit') {
						layIndex = openWindow('setting_data_form', "参数编辑", '500px', '500px', false, 0, false, false, data, 0.2, false, function(res){
							// 颜色
							colorpicker.render({
								elem: '#color-form'
							    ,color: res.dictColor?res.dictColor:'#65c0dc'
							    ,done: function(color){
							      $('#color-form-input').val(color);
							    }
							});
						})
					}
				});
				
				// 提交
				form.on('submit(submitSettingForm)',  function(data) {
					data.field.did = tempId;
					saveData(table, 'setting_table', data.field, interUrl.saveSetting, '#setting_data_form', null, layIndex);
					return false;
				})
				
				// 开关
				form.on('switch(stateSwitch)', function(data) {
					var id = $(this).attr('tid');
				    var state = data.elem.checked?"0":"1";
				    getAjax(interUrl.updateSettingState, {id: id, state: state}, 'post', function (res) {
				    	showTips(res.msg);
				    })
					return false;
				})
				
			})
		}
	}

	
})













